﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Reflection;

namespace Trends.TFC.Web.Controls
{

    public class MessagePanelControl : UserControl
    {

        protected List<string> isErrorList = new List<string>();
        protected List<string> isSucessList = new List<string>();

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.HasErrors)
            {
                HtmlGenericControl loDivMessagePanel = (HtmlGenericControl)this.LoadPageControl("divMessagePanel");
                if (loDivMessagePanel != null)
                {
                    string lsScript = "setTimeout(\"window.scrollTo(0,$('#{0}').offset().top)\", 0);";
                    lsScript = String.Format(lsScript, loDivMessagePanel.ClientID);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), this.UniqueID, lsScript, true);
                }
            }
        }

        public bool HasErrors
        {
            get { return this.isErrorList.Count > 0; }
        }

        public void AddMessageError(string asMessage)
        {
            this.isErrorList.Add(asMessage);
        }
        public void AddMessageSucess(string asMessage)
        {
            this.isSucessList.Add(asMessage);
        }

        public virtual void HideMessages()
        {
        }

        public virtual void ShowMessages(TipoOperacao aoTipoOperacao)
        {
        }
        public virtual void ShowErrors()
        {
        }
        public virtual void ShowSuccess()
        {
        }

        /// <summary>
        /// Retorna o objeto indicado por <c>asName</c> e pertencente ao controle <c>aoPage</c>.
        /// </summary>
        /// <param name="asName">Nome do objeto.</param>
        /// seja encontrado; caso contrário o método retorna null.</param>
        /// <returns></returns>
        protected internal Object LoadPageControl(string asName)
        {
            FieldInfo loField = this.GetType().GetField(asName, BindingFlags.Public |
              BindingFlags.NonPublic | BindingFlags.Instance);
            if (loField != null)
                return loField.GetValue(this);
            return null;
        }
    }

}
